********************************************************************************
* ELECTORAL REPERCUSSIONS OF A PANDEMIC:  EVIDENCE FROM THE 2009 H1N1 OUTBREAK *
********************************************************************************

// THIS FILE REPLICATES TABLE D1 IN APPENDIX D OF "ELECTORAL 
// REPERCUSSIONS OF A PANDEMIC: EVIDENCE FROM THE 2009 H1N1 OUTBREAK" BY
// EMILIO GUTIÉRREZ, JAAKKO MERILÄINEN, AND ADRIÁN RUBLI

global data "XXX" // Insert data directory here
global output "XXX" // Insert output directory here

* Open data

use "$data/GMR_night_lights.dta", clear

********************************************************************************

gen year2=year*year // Generate year^2

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

est clear

eststo: reghdfe nightlights 1.post##c.change_ari [aw=total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

keep if year==2006 | year==2009
gen a=1 if change_ari_z!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==2

eststo: reghdfe nightlights 1.post##c.change_ari_z [aw=total_votos] if year==2006 | year==2009, absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/2
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "
restore

preserve

gen a=1 if change_ari!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==7

eststo: reghdfe nightlights 1.post##c.change_ari [aw=total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/7
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari_z!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==7

eststo: reghdfe nightlights 1.post##c.change_ari_z [aw=total_votos], absorb(id i.munid#i.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/7
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==7

eststo: reghdfe nightlights 1.post##c.change_ari [aw=total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/7
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore


preserve

gen a=1 if change_ari_z!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==7

eststo: reghdfe nightlights 1.post##c.change_ari_z [aw=total_votos], absorb(id i.munid#i.year i.id#c.year) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/7
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear "\checkmark"
estadd local quadratic " "

restore

preserve

gen a=1 if change_ari!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==7

eststo: reghdfe nightlights 1.post##c.change_ari [aw=total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/7
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore

preserve

gen a=1 if change_ari_z!=. & nightlights!=.
bysort id: egen sum_a=sum(a)
keep if sum_a==7

eststo: reghdfe nightlights 1.post##c.change_ari_z [aw=total_votos], absorb(id i.munid#i.year i.id#c.year i.id#c.year2) cluster(clues)
estadd scalar N1 = e(N)
estadd scalar N2 = e(N)/7
estadd scalar N3 = e(N_clust)
estadd local district "\checkmark"
estadd local stateyear "\checkmark"
estadd local linear " "
estadd local quadratic "\checkmark"

restore

esttab est*  using "$output/tableD1.tex", replace noomitted nobaselevels booktabs  mlabels(none) ///
	compress lines star(* .1 ** .05 *** .01) label ///
	mgroups("2006 and 2009" "2006-2012", pattern(1 0 1 0 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	b(%9.5f) se(%9.5f) stats(N1 N2 N3 district stateyear linear quadratic, labels("Number of observations" "Number of sections" "Number of clusters" "Section FE" "Municipality-year FE" "Linear time trend" "Quadratic time trend") ///
	fmt(0 0 0)) keep(1.post#c.change_ari 1.post#c.change_ari_z) ///
	coeflabels(1.post#c.change_ari "1[Year $\geq$ 2009] $\times$ Excess ARI cases (10,000s)" ///
	1.post#c.change_ari_z "1[Year $\geq$ 2009] $\times$ Excess ARI cases (z-score)") ///
	order(1.post#c.change_ari 1.post#c.change_ari_z) ///
	nodepvars nomtitle prehead(\begin{tabular}{l*{9}{c}} \toprule) ///
	postfoot(\bottomrule \end{tabular})
